package com.culture.config.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import com.culture.config.common.util.BeanUtil;
import com.culture.config.dao.entity.RoleMenu;
import com.culture.config.dao.entity.RoleMenuPage;
import com.culture.config.dao.mapper.RoleMenuMapper;
import com.culture.config.service.RoleMenuService;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;
import com.culture.config.wrapper.RoleMenuWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RoleMenuServiceImpl implements RoleMenuService{
    @Resource
    private RoleMenuMapper roleMenuMapper;
    
    @Override
    public Page<RoleMenuVO> page(RoleMenuPageParam param) {
        log.info("page:{}",param);
        PageMethod.startPage(param.getPageNum(),param.getPageSize());
		Page<RoleMenuPage> roleList = this.roleMenuMapper.page(RoleMenuWrapper.wrapRoleMenuQuery(param));      
		return BeanUtil.copyPageProperties(roleList, RoleMenuVO.class);
    }    

    @Override
    @Transactional
    public void update(RoleMenuUpdatePO roleMenuUpdatePO) {
        log.info("update:{}",roleMenuUpdatePO);
        this.roleMenuMapper.delByRoleId(roleMenuUpdatePO.getRoleId());
        List<RoleMenu> addRoleMenus = RoleMenuWrapper.wrapRoleMenuListAdd(roleMenuUpdatePO);
        addRoleMenus.forEach(e->this.roleMenuMapper.add(e));
    }

    @Override
    public List<RoleMenuVO> listByRoleId(Long roleId) {
        List<RoleMenuPage> roleMenuPages = this.roleMenuMapper.listByRoleId(roleId);
        if(CollectionUtils.isEmpty(roleMenuPages)){
            return Collections.emptyList();
        }
        return BeanUtil.copyListProperties(roleMenuPages, RoleMenuVO.class);
    }
    
}
